package rickyGame.server.state
{
	import flash.geom.Point;
	
	import ricky.utils.TimeServer;
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	
	import rickyGame.game.skill.SkillData;
	import rickyGame.server.SpellController;
	
	/**
	 * 服务端按间隔执行魔法效果影响行动
	 */
	public class SpellInteravalAct extends Act2D
	{
		public function SpellInteravalAct(actor:Actor,skill:SkillData,target:Actor,point:Point)
		{
			super(actor);
			_type="SpellInteravalAct";
			this._target=target;
			this._point=point;
			this._skill=skill;
		}
		
		override public function begin():void
		{
			_startTime=TimeServer.now();
			_endTime=_startTime+_skill.continueTime;
			_interval=_skill.effectInterval;
			_last=_startTime;
			_state=1;

		}
		
		override public function action(timeline:Number):void
		{
			if(_state<1)
				return;
			
			var dt:Number=timeline-_last;
			if(dt>= _interval)//
			{
				_last=timeline;
				searchArea();
			}
			
			if(timeline>=_endTime)
			{
				this.complete();
			}
		}
		
		override public function end():void
		{
			super.end();
		}
		
		override public function dispose():void
		{
			_skill=null;
			_startTime=0;
			_endTime=0;
			super.dispose();
		}
		
		private function searchArea():void
		{
			var x:Number,y:Number;
			if(this._target)
			{
				x=_target.graphic.x;
				y=_target.graphic.y;
			}
			else if(this._point)
			{
				x=_point.x;
				y=_point.y;
			}
			
			var act:Act2D;
			var targets:Array=SpellController.findEffectTargets(this.actor,x,y,this._skill);
			
			if(targets)
			{
				act=SpellController.installState(actor,_skill,targets);
				SpellController.sendInstallStateToClient(act);
			}
		}
		
		//******************* private **********************
		protected var _point:Point;//搜索点
		protected var _target:Actor;//搜索目标
		protected var _last:Number; //上次执行时间
		protected var _interval:int;//执行间隔
		private var _startTime:Number;//开始时间毫秒数
		private var _endTime:Number;//持续时间毫秒数
		private var _skill:SkillData;//技能数据
		
	}
}